<?php
	getHeader();
	if (!isset($_SESSION['authPassed']))
	{
?>
		<span>你当前未登录, 无权进行关于队伍的一切操作.</span>
<?php
	}
	else 
	{
	  if (isset($_POST['action']))
	  {
	  	if ($_POST['action'] == 'create')
	  	{
	  		$data['teamName'] = $_POST['teamName'];
	  		$data['teamLeaderUid'] = $_SESSION['uid'];
	  		$data['teamCompetition'] = $_POST['cid'];
	  		$team = new teamProf($data);
	  		if ($team->create())
	  		{
?>
				<span>队伍创建成功</span>
<?php
	  		}
	  		else 
	  		{
?>
				<span>队伍创建失败</span>
<?php
	  		}
	  	}
	  	else if ($_POST['action'] == 'invite')
	  	{
	  		if (userProf::postMessage($_SESSION['userName'], $_POST['to'], '组队邀请', $_POST['content']) == 0)
	  		{
?>
				<span>邀请发送成功</span>
<?php
	  		}
	  		else 
	  		{
?>
				<span>邀请发送失败</span>
<?php
	  		}
	  	}
	  	else if ($_POST['action'] == 'request')
	  	{
	  		$i = teamProf::requestTeamInvite($_POST['tid'], $_SESSION['userName']);
	  		if ($i == 0)
	  		{
?>
				<span>入队申请发送成功</span>
<?php
	  		}
	  		else if ($i == 1)
	  		{
?>
	  			<span>你已经发送过入队申请.</span>
<?php
	  		}
			else 
	  		{
?>
				<span>入队申请发送失败</span>
<?php
	  		}
	  	}
	  	else if ($_POST['action'] == 'edit')
	  	{
	  		$team = teamProf::getTeamByTid($_POST['tid']);
	  		if ($team->leader == $_SESSION['uid'])
	  		{
	  			$team->introduction = $_POST['teamIntroduction'];
	  			if ($team->update())
	  			{
?>
					<span>修改成功</span>
<?php
	  			}
	  			else
	  			{
?>
					<span>修改失败</span>
<?php
	  			}
	  		}
	  	}
	  }
	  else if (isset($_GET['do']))
	  {
		if ($_GET['do'] == 'create')
		{
			if (isset($_GET['cid']))
			{
?>
				<form action='?p=editTeam' method='post'>
					<input type='hidden' name='action' value='create' />
					<input type='hidden' name='cid' value='<?php echo $_GET['cid']; ?>' />
					队名: <input type='text' name='teamName' /><br />
					<input type="submit" value="创建" class="button" />				
				</form>
<?php
			}
			else 
			{
?>
				<span>请从比赛入口开始指定一个比赛进行队伍创建.</span>
<?php
			}
		}
		else if ($_GET['do'] == 'invite')
		{
			if (isset($_GET['tid']))
			{
				$team = teamProf::getTeamByTid($_GET['tid']);
				if ($team->leader == $_SESSION['uid'])
				{
					$link = teamProf::createInviteCode($team->tid);
					if ($link == 'NO') 
					{
?>
						<span>系统错误, 生成邀请码失败.</span>
<?php
					}
					else
					{
?>
					  <form action='?p=editTeam' method='post'>
						<input type='hidden' name='action' value='invite' />
						<input type='hidden' name='from' value='<?php echo $_SESSION['userName']; ?>' />
						邀请成员: <input type='text' name='to' /><br />
						<textarea name='content' id='tinyInput'>我是<?php echo $team->name; ?>的队长<?php echo $_SESSION['userName']; ?>, 我们的队伍正在参加"<?php echo compProf::getCompetitionNameByCid($team->competition); ?>", 诚邀你的加入. 请点击这个<?php echo $link; ?>加入我们.</textarea>
						<?php getEditor(); ?>
						<input type="submit" value="发送邀请" class="button" onclick="instance.post();" />				
					  </form>
<?php
					}
				}
				else
				{
?>
				<span>你无权邀请成员加入.</span>
<?php
				}
			}
			else
			{
?>
				<span>请从队伍入口开始指定一个队伍进行邀请.</span>
<?php
			}
		}
		else if ($_GET['do'] == 'request')
		{
			if (isset($_GET['tid']))
			{
				$team = teamProf::getTeamByTid($_GET['tid']);
				if ($team->leader == $_SESSION['uid'] || strstr($team->member, ",{$_SESSION['uid']},"))
				{
?>
					<span>你当前已经为该队成员.</span>
<?php
				}
				else
				{
?>
					<form action='?p=editTeam' method='post'>
						<input type='hidden' name='action' value='request' />
						<input type='hidden' name='tid' value='<?php echo $_GET['tid']; ?>' />
						你是否要加入队伍: <?php echo $team->name; ?><br />
						<input type="submit" value="确定" class="button" />				
					</form>
<?php
				}
			}
			else
			{
?>
				<span>请从队伍入口开始指定一个队伍发送入队申请.</span>
<?php
			}
		}
		else if ($_GET['do'] == 'memberManage')
		{
			if (isset($_GET['tid']))
			{
				$team = teamProf::getTeamByTid($_GET['tid']);
				if ($team->leader == $_SESSION['uid'])
				{
?>
					<span>队名: <?php echo $team->name; ?></span><br />
					<span>队员: </span><br />
					<ul>
<?php
						$member = getMemberLink($team->member,FALSE);
						foreach ($member as $i)
						{
?>
							<li><?php echo $i; ?></li>
<?php
						}
?>
					</ul>
					<span>申请加入的用户: </span><br />
					<ul>
<?php
						$member = $team->getRequestUser();
						foreach ($member as $i)
						{
?>
							<li><a href='?p=search&type=userName&userName=<?php echo $i; ?>'><?php echo $i; ?></a>&nbsp;&nbsp;&nbsp;&nbsp;<a href='?p=rancode&'></a></li>
<?php
						}
?>
					</ul>
<?php
				}
			}
		}
		else if ($_GET['do'] == 'edit')
		{
			$team = teamProf::getTeamByTid($_GET['tid']);
			if ($team->leader == $_SESSION['uid'])
			{
?>
				<form action="?p=editTeam" method="post">
					<input type='hidden' name='action' value='edit' />
					<input type='hidden' name='tid' value='<?php echo $team->tid;?>' />
					队伍介绍: <textarea name="teamIntroduction" id="tinyInput"><?php echo $team->introduction; ?></textarea><?php getEditor();?>
					<input type="submit" value="提交" onclick="instance.post();">
				</form>
<?php
			}
		}
	  }
	}
	getPanel();
	getFooter();
?>